package com.jiliang.common.app;


import com.alibaba.fastjson2.JSONObject;
import com.jiliang.common.json.JsonDsUtil;
import com.jiliang.common.string.StringCheckDsUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 * @version v_1.0.1
 * @since 2016/11/4
 */
public class UserAgentDsUtil {

    private static Logger log = LoggerFactory.getLogger(UserAgentDsUtil.class);

    public static final String UA_APPCONNECT = "OAPPConnect";
    public static final String UA_ZNTCONNECT = "OAConnect";
    public static final String UA_IPHONE = "iPhone";
    public static final String UA_ANDROID = "Android";
    public static final String UA_WINDOWS = "Windows";
    public static final String UA_MACINTOSH = "Macintosh";
    public static final String UA_MICROMESSENGER = "MicroMessenger";
    public static final String UA_IPAD = "iPad";

    public static final String UA_TYPE_APP = "app";
    /**
     * 设备
     */
    public static final String DEVICE_IPHONE = "iPhone";
    public static final String DEVICE_ANDROID = "Android";
    public static final String DEVICE_WINDOWS = "Windows";
    public static final String DEVICE_MACINTOSH = "Macintosh";
    public static final String DEVICE_IPAD = "iPad";
    /**
     * 客户端
     */
    public static final String CLIENT_MOB = "MOB";
    public static final String CLIENT_PC = "PC";
    public static final String CLIENT_APP = "APP";
    public static final String CLIENT_WX = "WX";
    /**
     * 设备系统
     */
    public static final String DEVICE_SYSTEM_ANDROID = "Android";
    public static final String DEVICE_SYSTEM_IOS = "iOS";
    public static final String DEVICE_SYSTEM_WINDOWS = "Windows";

    /**
     * 识别系统型号
     *
     * @param request
     * @return
     */
    public static String getClientModel(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        log.info("设备User-Agent:" + ua);
        String type = "";
        if (ua.contains(UA_APPCONNECT) || ua.contains(UA_ZNTCONNECT)) {
            type = UA_TYPE_APP;
        } else {
            type = getDevice(request);
        }
        return type;
    }

    /**
     * 获取客户端
     *
     * @param request
     * @return java.lang.String
     * @author ZhuPengFei
     * @since 2018/12/12 20:53
     */
    public static String getClient(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (ua.contains(UA_APPCONNECT) || ua.contains(UA_ZNTCONNECT)) {
            return CLIENT_APP;
        }
        if (ua.contains(UA_WINDOWS) || ua.contains(UA_MACINTOSH)) {
            return CLIENT_PC;
        }
        if (ua.contains(UA_MICROMESSENGER)) {
            return CLIENT_WX;
        }
        return CLIENT_MOB;
    }

    /**
     * 获得终端设备
     *
     * @param request
     * @return java.lang.String
     * @author YanShuang
     * @since 2019/1/10 17:50
     */
    public static String getDevice(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        String type = "";
        if (ua.contains(UA_IPHONE)) {
            type = DEVICE_IPHONE;
        } else if (ua.contains(UA_ANDROID)) {
            type = DEVICE_ANDROID;
        } else if (ua.contains(UA_WINDOWS)) {
            type = DEVICE_WINDOWS;
        } else if (ua.contains(UA_MACINTOSH)) {
            type = DEVICE_MACINTOSH;
        } else if (ua.contains(UA_IPAD)) {
            type = DEVICE_IPAD;
        } else {
            type = ua;
        }
        log.info("设备型号：" + type);
        return type;
    }

    /**
     * 获取终端设备系统
     *
     * @param request
     * @return java.lang.String
     * @author YanShuang
     * @since 2019/4/2 14:37
     */
    public static String getDeviceSystem(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (ua.contains(UA_IPHONE)) {
            return DEVICE_SYSTEM_IOS;
        } else if (ua.contains(UA_ANDROID)) {
            return DEVICE_SYSTEM_ANDROID;
        } else if (ua.contains(UA_WINDOWS)) {
            return DEVICE_SYSTEM_WINDOWS;
        }
        JSONObject jsonObject = null;
        if (ua.contains(UA_APPCONNECT) || ua.contains(UA_ZNTCONNECT)) {
//            jsonObject = JsonDsUtil.jsonStrToJSONObject(ua);
            jsonObject = JsonDsUtil.jsonStrToJSONObject(ua);
        }
        Object os = jsonObject == null ? null : jsonObject.get("os");
        if (os != null) {
            String client = (String) os;
            return client;
        }
        return "";
    }

    /**
     * 判断是否是移动端
     *
     * @param request
     * @return boolean
     * @author YanShuang
     * @since 2018/9/14 10:32
     */
    public static boolean isMob(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (ua.contains(UA_WINDOWS) || ua.contains(UA_MACINTOSH)) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否是移动端
     *
     * @param client
     * @return boolean
     * @author YanShuang
     * @since 2018/9/14 10:32
     */
    public static boolean isMob(String client) {
        if (StringUtils.isBlank(client) || StringUtils.isNotBlank(client) && client.equals(CLIENT_PC)) {
            return false;
        }
        return true;
    }

    /**
     * 是否是APP
     *
     * @param request
     * @return boolean
     * @author YanShuang
     * @since 2018/9/16 14:46
     */
    public static boolean isApp(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (ua.contains(UA_APPCONNECT) || ua.contains(UA_ZNTCONNECT)) {
            return true;
        }
        return false;
    }

    /**
     * 是否智农通
     *
     * @param request
     * @return boolean
     * @author ZhuPengFei
     * @since 2019/5/20 15:19
     */
    public static boolean isZnt(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (ua.contains(UA_ZNTCONNECT)) {
            return true;
        }
        return false;
    }

    /**
     * 获取关于我们版本号
     *
     * @param request
     * @return java.lang.String
     * @author baidongxue
     * @date 2018/9/18
     */
    public static String getAppVersion(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        if (StringCheckDsUtil.isBlank(ua)) {
            return null;
        }
        String appVersion = null;
        try {
            JSONObject object = JsonDsUtil.jsonStrToJSONObject(ua);
            appVersion = object.getString("appVersion");
        } catch (Exception e) {
            log.info("get app version error, message : {}", e.getMessage());
        }
        log.info("当前app版本号为：{}", appVersion);
        return appVersion;
    }
}